<template>
  <div>
    <el-upload
      ref="importUpload"
      class="upload-demo"
      action="api/course/upload/"
      :on-success="onUploadSuccess"
      accept=".png, .jpg, .jpeg"
      :on-preview="handlePreview"
      :on-remove="handleRemove"
      :before-remove="beforeRemove"
      list-type="picture-card"
      multiple
      :limit="1"
      :on-exceed="handleExceed"
      :file-list="fileList">
      <i slot="default" class="el-icon-plus"></i>
      <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible" :modal-append-to-body="false">
      <img width="100%" :src="dialogImageUrl" alt="404">
    </el-dialog>
  </div>
</template>

<script>
 export default {
   props:['fp','fn'],
    data() {
      return {
        fileList:[],
        filepath:'',
        filename:'',
        dialogVisible:false,
        dialogImageUrl:'',
      };
    },
   mounted:function () {
     this.getFileData()
   },
   watch: {
     fp(newValue){
       if (newValue !== ''){
         this.fileList.push({name:newValue.substring(17),url:newValue})
         console.log(newValue)
       }else {
         this.fileList=[]
       }
     },
     // fn(newValue){
     //   if (newValue !== ''){
     //     this.fileList.push({name:newValue})
     //     console.log(newValue)
     //   }else {
     //     this.fileList=[]
     //   }
     // }
   },
   methods: {
     getFileData() {
       if (this.fp){
         this.fileList.push({name:this.fp.substring(17),url:this.fp})
       }else {
         this.fileList=[]
       }
     },
      onUploadSuccess(msg) {
        this.filepath = msg['filepath']
        this.filename = msg['filename']
        this.$emit('imgChange',this.filepath,this.filename)
      },
      handleRemove(file, fileList) {
        console.log(file, fileList);
      },
      handlePreview(file) {
       this.dialogImageUrl=this.fp
       this.dialogVisible=true;
      },
      handleExceed(files, fileList) {
        this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
      },
      beforeRemove(file, fileList) {
        return this.$confirm(`确定移除 ${ file.name }？`);
      }
    }
  }
</script>

<style scoped>

</style>
